
class UploadedtoPlugin:
	def __init__(self):
		# put all the links this plugins supports here - it will be used to filter supported URLs
		self.supported_sites = ['uploaded.to']
		self.supported_urls = self.supported_sites
		self.cookies_to_block = ['.uploaded.to']
		# whether we support pause/resume
		self.supports_resume = False
		# maximum number of concurrent downloads
		self.max_concurrent = 1
		# parameter names we require
		self.param_names = []
		# parameter values
		self.param_vals = {}
		self.name = self.supported_sites[0]

	def get(self, url):
		"""Get a single file from www.uploaded.to"""
		post_pat = 'post" action="'
		titl_pat = '<title>'
		local_file = None
		remote_url = None
		first_try = True
		while True:
			data = get_url_data(url)
			account_blocked = False
			# Find the line with our file location
			for line in data.splitlines():
				loc = line.find('Or wait')
				if post_pat in line:
					remote_url = line.split(post_pat)[1].split('"')[0]
					break
				elif titl_pat in line:
					local_file = line.split(' ...')[0].split(titl_pat)[1].strip()
				elif loc != -1:
					line = line[loc:].split(' ')
					sleep_time = int(line[2])
					if 'minute' in line[3]:
						sleep_time *= 60
					elif 'hour' in line[3]:
						sleep_time *= 3600
					if sleep_time == 0:
						sleep_time = 15 * 60
					sleep_time += 1
					account_blocked = True
					break
			if account_blocked:
				if first_try:
					if not sleep_interruptible(sleep_time):
						return
					config.report.report_warning('waking up and attempting to download again')
				else:
					raise CantDownload("Unable to download from uploaded.to even after %d minutes sleep" % (sleep_time / 60))
			else:
				break
			first_try = False

		if not remote_url or not local_file:
			raise CantDownload('Error occured while trying to download')

		content = download_file_with_retries(local_file, remote_url, True)
		config.report.report_finish(local_file)

	def url_matches(self, url):
		"""Check whether we support this URL"""
		for l in self.supported_urls:
			if l in url:
				return True
		return False

	def version(self):
		"""Return plugin version - when interface changes, the version changes"""
		return 2

modules = [UploadedtoPlugin()]
